/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package group6renderer;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFileChooser;
import java.awt.*;
import group6renderer.renderernode.*;
import java.io.File;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author jonnie
 */
public class Group6Renderer
{
	public static File htmlFile;
	public static final String NL = "/r/n";
	public static final String DIRSEP = "\\";
	public static TreeNode treeRoot;
	private String html;
	GUI theGUI;
    private Parser theParser;
    public static Renderer theRenderer;
   

    Group6Renderer()
    {
    	HTMLConstants.buildHTMLConstants();
    	html = parseFile();
    	html = html.replaceAll("%20", " ");
    	theParser = new Parser(html);
    	treeRoot = theParser.parse();
    	ErrorLog.errorInstance().sendErrorsToFile();
    	theRenderer = new Renderer(treeRoot);

    	GUI theGUI = GUI.guiInstance();


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
	Group6Renderer project = new Group6Renderer();

    }

    public String parseFile()
    {
	JFileChooser fc = new JFileChooser();
	fc.setFileFilter(new FileFilter()
	{

	    public String getDescription()
	    {
		return "HTML files";
	    }

	    public boolean accept(File f)
	    {
		if (f.isDirectory())
		{
		    return false;
		}
		if (f.getName().endsWith(".htm"))
		{
		    return true;
		}
		if (f.getName().endsWith(".html"))
		{
		    return true;
		}
		return false;
	    }
	});
	if (fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
	{
	    System.exit(0);
	}
	FileReader theFile = null;
	htmlFile = fc.getSelectedFile();
	StringBuffer sb = new StringBuffer();
	try
	{
	    theFile = new FileReader(htmlFile);
	    for (int i = 0; i <= fc.getSelectedFile().length(); i++)
	    {
		try
		{
		    sb.append((char) theFile.read());
		}
		catch (IOException ex)
		{
		    Logger.getLogger(Group6Renderer.class.getName()).log(Level.SEVERE, null, ex);
		}
	    }
	}
	catch (FileNotFoundException ex)
	{
	    Logger.getLogger(Group6Renderer.class.getName()).log(Level.SEVERE, null, ex);
	}

	return sb.toString();
    }
}
